#!/bin/sh

. /etc/rc.common

# idempiere Service script for OS/X
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
export POSTGRES_HOME=/Library/PostgreSQL/8.3
export IDEMPIERE_HOME=/Applications/idempiere
export PATH=$PATH:$JAVA_HOME/bin/:$POSTGRES_HOME/bin/
EXECDIR=/Applications/iDempiere
ENVFILE=/etc/profile
RETVAL=0
IDEMPIERESTATUS=
MAXITERATIONS=60 # 2 seconds every iteration, max wait 2 minutes)

RunService ()
{
    case $1 in
      start  ) StartService   ;;
      stop   ) StopService    ;;
      restart) RestartService ;;
      status ) StatusService ;;
      *      ) echo "$0: unknown argument: $1";;
    esac
}


StatusService ()
{
    IDEMPIERESTATUSSTRING=$(ps ax | grep -v grep | grep server| grep $EXECDIR)
    echo $IDEMPIERESTATUSSTRING | grep $EXECDIR 
    #IDEMPIERESTATUS=$?
}

StartService ()
{
    IDEMPIERESTATUS=$(ps ax | grep -v grep | grep server| grep $EXECDIR|wc -l)
    if [ $IDEMPIERESTATUS != 0 ] ; then
        ConsoleMessage "idempiere is already running"
        return 1
    fi
    ConsoleMessage "Starting iDempiere ERP: "
    export LOGFILE=$IDEMPIERE_HOME/log/idempiere_`date +%Y%m%d%H%M%S`.log
    export LOOKFILE=$IDEMPIERE_HOME/server.log
    su root -c "cd $EXECDIR;$EXECDIR/idempiere-server.sh &> $LOGFILE &"
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        # wait for server to be confirmed as started in logfile
        STATUSTEST=0
        ITERATIONS=0
        while [ $STATUSTEST -eq 0 ] ; do
            sleep 2
            tail -n 5 $LOOKFILE | grep 'Server\].*Started in' &> /dev/null && STATUSTEST=1
            ITERATIONS=`expr $ITERATIONS + 1`
            if [ $ITERATIONS -gt $MAXITERATIONS ]
            then
                break
            fi
        done

    if [ $STATUSTEST -eq 0 ]
        then
            ConsoleMessage "Service hasn't started within the timeout allowed, please review file $LOGFILE to see the status of the service"
        else
            ConsoleMessage "idempiere Started"
        fi
        echo
    else
        ConsoleMessage "idmpiere Failed"
        echo
    fi
    return $RETVAL
}

StopService()
{
    IDEMPIERESTATUS=$(ps ax | grep -v grep | grep server| grep $EXECDIR|wc -l)
    if [ $IDEMPIERESTATUS = 0 ] ; then
        echo "idempiere is already stopped"
        return 1
    fi
    ConsoleMessage "Stopping iDempiere ERP: "
    RETVAL=$?
    # try direct kill with signal 15, then signal 9
    kill -15 `ps ax | grep -v grep | grep $EXECDIR | sed -e 's/^ *//g' | cut -f 1 -d " "`
    sleep 5
    IDEMPIERESTATUS=$(ps ax | grep -v grep | grep server| grep $EXECDIR|wc -l)
    if [ $IDEMPIERESTATUS -ne 0 ] ; then
	  ConsoleMessage "Service stopped with kill -15"
    else
	  echo "Trying direct kill with signal -9"
	  kill -9 `ps ax | grep -v grep | grep $EXECDIR | sed -e 's/^ *//g' | cut -f 1 -d " "`
	  sleep 5
	  IDEMPIERESTATUS=$(ps ax | grep -v grep | grep server| grep $EXECDIR|wc -l)
	  if [ $IDEMPIERESTATUS -ne 0 ] ; then
	    ConsoleMessage "Service stopped with kill -9"
	  else
	    ConsoleMessage "Service hasn't stopped"
	  fi
    fi
    return $RETVAL
}


RestartService ()
{
    StopService
    sleep 2
    StartService
}


RunService "$1"
